package com.central.rearService.service.serviceImp

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.util.ObjectUtil
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.central.common.entity.Report
import com.central.common.enum.ResultCode
import com.central.common.feign.PostService
import com.central.common.feign.UserService
import com.central.common.model.*
import com.central.common.service.impl.SuperServiceImpl
import com.central.post.pojo.report.GetReportListDto
import com.central.post.pojo.report.GetReportListVo
import com.central.post.pojo.report.ReportInsertDto
import com.central.rearService.mapper.ReportMapper
import com.central.rearService.service.ReportService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * @author  fly
 * @date  2021/5/26 14:54
 * @version 1.0
 */
@Service
open class ReportServiceImp : SuperServiceImpl<ReportMapper, Report>(), ReportService {

    @Autowired
    private lateinit var postService: PostService

    @Autowired
    private lateinit var userService: UserService


    override fun addReport(reportInsertDto: ReportInsertDto, sysUser: SysUser): CommonResult<Boolean> = success(
        save(Report().apply {
            BeanUtil.copyProperties(reportInsertDto, this)
            createBy = sysUser.id
        })
    , "举报成功")

    override fun remove(ids: String): CommonResult<Boolean> = success(removeByIds(ids.split(",")), "批量移除成功")

    override fun getReportList(
        pageVo: PageVo,
        getReportListDto: GetReportListDto,
        sysUser: SysUser
    ): CommonResult<Page<GetReportListVo>> = page(Page(pageVo.pageNumber!!, pageVo.pageSize!!), KtQueryWrapper(Report::class.java)
        .eq(ObjectUtil.isNotEmpty(getReportListDto.type), Report::type, getReportListDto.type)
        .like(StrUtil.isNotBlank(getReportListDto.reason), Report::reason, getReportListDto.reason))
        .run {
            records.takeIf { it.isNotEmpty() }?.let {
                success(Page<GetReportListVo>(current, size, total, isSearchCount).apply {
                    records = it.mapNotNull { getReportVo(it) }
                }, "获取举报列表成功")
            } ?: failed(ResultCode.NO_CONTENT)
        }

    private fun getReportVo(report: Report): GetReportListVo = GetReportListVo().apply {

        postService.getPostDetail(report.postId!!).let {
            postId = it?.id
            content = it?.content
            pics = it?.pics?.split(",")
        }
        typeStr = when(report.type) {
            0 -> "涉污涉黄"
            1 -> "性感过度"
            2 -> "不友善行为"
            3 -> "骚扰广告"
            4 -> "违法违规"
            else -> "其他违规"
        }
        nickname = userService.selectByUserId(report.createBy)?.username
        BeanUtil.copyProperties(report, this)
    }
}